<?php

/*
 * This file is part of ibrand/EC-Open-Server.
 *
 * (c) iBrand <https://www.ibrand.cc>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace iBrand\EC\Open\Server\Http\Controllers;

use iBrand\Component\Advert\Repository\AdvertItemRepository;
use iBrand\EC\Open\Backend\Store\Repositories\CategoryRepository;
use iBrand\EC\Open\Backend\Store\Repositories\GoodsRepository;
use iBrand\EC\Open\Core\Services\GoodsService;
use Illuminate\Support\Facades\DB;

class HomeController extends Controller
{
    private $advertItem;

    public function __construct(AdvertItemRepository $advertItemRepository,GoodsRepository $goodsRepository,CategoryRepository $categoryRepository)
    {
        $this->advertItem = $advertItemRepository;
        $this->goodsRepository = $goodsRepository;
        $this->categoryRepository = $categoryRepository;

    }

    public function category()
    {
        $items = $this->advertItem->getItemsByCode('home.categories');

        return $this->success($items);
    }


    /**
     * @return \Dingo\Api\Http\Response
     * 小程序首页列表
     */
    public function reindex()
    {
        $banner_list = DB::table('admin_banner')->where(['is_valid'=>1])->orderBy('sort','asc')->get();
        foreach ($banner_list as &$value){
            if($value->link_url=='/pages/shop/shop'){
                $value->type=2;
            }
        }
        //getCommendGoods
        $goodsList = DB::select("SELECT `id`,`name`,`img`,`sell_price`,`sub_name` FROM `ibrand_goods` WHERE `id` in (52,37,38,35) ORDER BY FIELD(`id`,52,37,38,35)");
        foreach ($goodsList as &$v){
            if($v->id==52){
                $v->img = 'http://shop.pavomea.com/storage/images/20190315/DVQKbWpTPN.jpg';
            }elseif ($v->id==37){
                $v->img = 'http://shop.pavomea.com/storage/images/20190315/SKewolvj4X.jpg';
            }elseif ($v->id==38){
                $v->img = 'http://shop.pavomea.com/storage/images/20190315/eqYV3Wve6O.jpg';
            }else{
                $v->img = 'http://shop.pavomea.com/storage/images/20190414/YtP99vqVAR.jpeg';
            }
        }
        $data = ['banner_list'=>$banner_list,'goods_list'=>$goodsList];
        return $this->success($data);
    }


    public function accesstoken(){
        $officialAccount = EasyWeChat::officialAccount();
        $token = $officialAccount->access_token->getToken();
        Redis::setex('lm_access_token_bengong_open',7000,$token['access_token']);
    }


    public function test(){
        //
        $parent_discount = DB::table(config('admin.database.menu_table'))->insertGetId([
            'parent_id' => 9,
            'order' => 14,
            'title' => '活动管理',
            'icon' => 'iconfont icon-cuxiaoguanli',
            'blank' => 1,
            'uri' => '',
            'created_at' => date('Y-m-d H:i:s', time()),
            'updated_at' => date('Y-m-d H:i:s', time()),
        ]);
        $discount_menu_list = [
            ['parent_id' => $parent_discount,
                'order' => 15,
                'title' => '拼团活动管理',
                'icon' => '',
                'blank' => 1,
                'uri' => 'store/activity/pintuan',
                'created_at' => date('Y-m-d H:i:s', time()),
                'updated_at' => date('Y-m-d H:i:s', time())]
        ];
        DB::table(config('admin.database.menu_table'))->insert($discount_menu_list);
    }

    //商城首页
    public function shopIndex(){
        //查询分类
        $category = $this->categoryRepository->getSortCategory();
        $category[0]['c_image'] = 'http://shop.pavomea.com/storage/images/20190222/eZAnRU2P5Z.jpg';
        $category[1]['c_image'] = 'http://shop.pavomea.com/storage/images/20190222/A6bmnsIMcf.jpg';
        $category[2]['c_image'] = 'http://shop.pavomea.com/storage/images/20190413/euY3FMB5in.png';
        $category[3]['c_image'] = 'http://shop.pavomea.com/storage/images/20190222/oPO3NTSoAb.jpg';
        $category[4]['c_image'] = 'http://shop.pavomea.com/storage/images/20190222/2LZ5gtz5Ed.jpg';
        $category[5]['c_image'] = 'http://shop.pavomea.com/storage/images/20190222/CmnCQmhtjF.jpg';
        $category[6]['c_image'] = 'http://shop.pavomea.com/storage/images/20190222/txY7OlromC.jpg';


        //var_dump($category);
        $news = [];
        $news[] = ['title'=>'本宫是谁？你还不知道？','path'=>'https://mp.weixin.qq.com/s/uSM0X4KZezIkbTmtqkUVWg'];
        $news[] = ['title'=>'「秘密花园」最强玩法揭秘！','path'=>'https://mp.weixin.qq.com/s/Y62DFScz0-Z7FJ0-mHAXBw'];
        $news[] = ['title'=>'快来「研食星球」打开零食王国的大门！','path'=>'https://mp.weixin.qq.com/s/J4Bcg_06SZpDiTbOqS8EFg'];


        $goodsList = $this->goodsRepository->scopeQuery(function ($query) {
            $query->where('newcomer_price','=',NULL);
            return $query->where(['is_del' => 0, 'is_largess' => 0]);
        })->orderBy('sale_count', "desc")->simplePaginate(10,['id','name','img','sell_price','sub_name','market_price']);
        $goods = $goodsList?json_decode(json_encode($goodsList),true):['data'=>[]];

        $data = ['category'=>$category,'news'=>$news,'goods_list'=>$goods['data']];
        return $this->success($data);
    }
}
